﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Model;
using University.Workshop.Business;
using University.Workshop.DataAccess;
using System.Web.Security;

namespace University.Workshop.WebApp
{
    public partial class Update : System.Web.UI.Page
    {
        private static int number=0;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadImage(UserDataAccess.GetUserByEmail(User.Identity.Name).Id);
            if (number == 0)
            {
                LblError.Visible = false;
                User user = UserDataAccess.GetUserByEmail(User.Identity.Name);
                TxtName.Text = user.PersonalInformation.FirstName;
                TxtLastName.Text = user.PersonalInformation.LastName;
                TxtEmail.Text = user.ContactInformation.Email;
                TxtPhone.Text = user.ContactInformation.Phone.ToString();
                TxtMobilePhone.Text = user.ContactInformation.MobilePhone.ToString();
                TxtCity.Text = user.ContactInformation.City;
                for(int i=0;i<9;i++)
                {
                    if (DropDownList1.Items[i].Text == user.ContactInformation.Department)
                        DropDownList1.SelectedIndex = i;
                }
                number = 1;
            }
        }
        protected void ButtonUpdateUser_Click(object sender, EventArgs e)
        {
            
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                FormsAuthentication.RedirectToLoginPage();
            }
            else
            {
                string path = newImage.Text;
                if (!string.IsNullOrEmpty(path))
                {
                    ImageUploadHandler.UploadHavingPath(path, Server, UserDataAccess.GetUserByEmail(User.Identity.Name).Id);
                    DeleteImage(UserDataAccess.GetUserByEmail(User.Identity.Name).Id);

                }
                LblError.Visible = false;
                try
                {
                    UserUpdateManager.UpdateUser(TxtPhone.Text, TxtMobilePhone.Text, TxtCity.Text, DropDownList1.SelectedItem.Text, User.Identity.Name);
                    LblError.Visible = true;
                    LblError.ForeColor = System.Drawing.Color.Blue;
                    LblError.Text = "Changes saved";
                    number = 0;
                }
                catch (Exception ex)
                {
                    LblError.Visible = true;
                    LblError.Text = ex.Message;
                }
            }
        }
        public void LoadImage(long userId)
        {

            //if (Directory.Exists(Server.MapPath("~/Thumbnails/U") + userId + "/"))
            //{
            //    DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Thumbnails/U") + userId + "/");
            //    FileInfo[] currentImages = dir.GetFiles();
            //    string thumbUrl = "~/Thumbnails/U" + userId + "/";
            //    foreach (FileInfo fi in currentImages)
            //    {
            //        if (String.IsNullOrEmpty(image1.ImageUrl))
            //        {
            //            image1.ImageUrl = thumbUrl + fi.Name;

            //        }
            //    }
            //}
            //else
            //{
            //    image1.ImageUrl = "~/Thumbnails/NoImage/noimage.jpg";
            //}
        }

        private void DeleteImage(long userId)
        {
            //string thumbDir = Server.MapPath("~/Thumbnails/U") + userId + "/";
            //string imageToDelete = string.Empty;
            //string name = Path.GetFileName(image1.ImageUrl);
            //FileInfo image = new FileInfo(thumbDir + name);
            //if (image.Exists)
            //{
            //    imageToDelete = name;
            //}

            //ImageUploadHandler.DeleteUserImage(imageToDelete, Server, userId);
        }
    }
}
